﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using wf=System.Windows.Forms;
using System.IO;
using WPF_DbCenter.CommonMethod;

namespace WPF_DbCenter.SqlCE.UI
{
    /// <summary>
    /// Interaction logic for GenerateLinqContext.xaml
    /// </summary>
    public partial class GenerateLinqContext : Window
    {

        public bool IsUsingWhenNotLogin { get; set; }

        public GenerateLinqContext()
        {
            InitializeComponent();
            ShowInTaskbar = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="exeFile">Exe File</param>
        /// <param name="sdfFile">Sqlce DbFile </param>
        /// <param name="CodeFile">Generated Code File</param>
        /// <param name="codeFile">Database password</param>
        private void DoGenerateLinqContext(string exeFile, string sdfFile, string codeFile,string pwd)
        {

            string arguments = string.Empty;
            if (string.IsNullOrEmpty(pwd))
            {
                arguments = string.Format(" {0} /code:{1}", sdfFile, codeFile);
            }
            else
            {
                arguments = string.Format(" {0} /password:{2} /code:{1}", sdfFile, codeFile, pwd);
            }
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = exeFile;
            info.Arguments = arguments;
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = false;
            info.RedirectStandardOutput = true;

            try
            {
                Process process = System.Diagnostics.Process.Start(info);
                process.WaitForExit();
                string stdout = process.StandardOutput.ReadToEnd();
                StringBuilder output = new StringBuilder();
                output.Append((stdout == "") ? "" : string.Format("\n\nOutput:\n{0}", stdout));
                string outputString = output.ToString();

                if (File.Exists(codeFile))
                {
                    MessageBox.Show("Generate Linq Context Succesful ,the output code file name is \r\n " + codeFile);
                }
                else
                {
                    MessageBox.Show(stdout);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        /// <summary>
        /// Select Db File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            using (wf.OpenFileDialog of = new System.Windows.Forms.OpenFileDialog())
            {
                of.Filter = "SqlCe Db File(*.sdf)|*.sdf|All Files(*.*)|*.*";
                if (of.ShowDialog() == wf.DialogResult.OK)
                {
                    textBox1.Text = of.FileName;
                }
            }
        }

        /// <summary>
        /// Do
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butGenLinqContext_Click(object sender, RoutedEventArgs e)
        {
            string exe = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Appendix\\Sqlmetal.exe";
            if (!File.Exists(exe))
            {
                MessageBox.Show("File Lost ,Please Reinstall");
                return;
            }
            string sdfFile = textBox1.Text;
            if (string.IsNullOrEmpty(sdfFile))
            {
                MessageBox.Show("Please select sqlce db file first");
                return;
            }

            string targetCodeFile = System.IO.Path.GetDirectoryName(sdfFile) +"\\"+ System.IO.Path.GetFileNameWithoutExtension(sdfFile) + ".cs";
            if (File.Exists(targetCodeFile))
            {
                MessageBox.Show(string.Format("The output file existed which is {0}",targetCodeFile));
                return;
            }
            string pwd = passwordBox1.Password;
            DoGenerateLinqContext(exe, sdfFile, targetCodeFile,pwd);

        }

        private void genLinqContextOfCurrentOpendDb_Click(object sender, RoutedEventArgs e)
        {
            //if (string.IsNullOrEmpty(((CEEnginer)App.MainEngineer).CurPwd))
            //{
            //    MessageBox.Show("Please input current database password");
            //    return;
            //}
            passwordBox1.Password = ((CEEnginer)App.MainEngineer).CurPwd;
            textBox1.Text = ((CEEnginer)App.MainEngineer).CurOpeningDbName;
            butGenLinqContext_Click(null, null);
        }

    }
}
